import GameCfgData from "../lib/GameCfgData";
import Sdk from "../lib/Sdk";
import PlayerData from "../com/data/PlayerData";

export default class GameIcons extends Laya.Script {
    private filterAppid:string="wxbd5459b7ecc54ec2";
    private games=[];
    constructor() { super(); }
    
    onEnable(): void {
        this.owner.getChildByName("list")["renderHandler"]=new Laya.Handler(this,this.listranderr);
        this.owner.getChildByName("list")["mouseHandler"]=new Laya.Handler(this,this.click);
        this.games=GameCfgData.getIns().data.games;
        if(this.filterAppid)this.filterid()
        this.owner.getChildByName("list")["array"]=this.games;
    }

    private listranderr(_sp:Laya.Sprite,_index:number){
        let tdata = this.games[_index];
        _sp.getChildByName("icon")["texture"]=tdata.icon;
        _sp.getChildByName("nameTxt")["text"]=tdata.name;
    }

    private click(e:Laya.Event,_index:number){
        if(e.type==Laya.Event.CLICK){
            let tdata = this.games[_index];
            Sdk.getIns().openGame(tdata);
            PlayerData.getIns().setDayTaskAwardSt(4,1);
        }
    }

    onDisable(): void {
        this.owner.off(Laya.Event.CLICK,this,this.click);
    }

    private filterid(){
        for(let i=0;i<this.games.length;i++){
            if(this.games[i].appId==this.filterAppid){
                this.games.splice(i,1);
                break;
            }
        }
    }
}